package com.ghxxzx.dagl.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ghxxzx.dagl.entity.Dalx;
import com.ghxxzx.dagl.exceptions.JyException;
import com.ghxxzx.dagl.mapper.DalxMapper;
import com.ghxxzx.dagl.service.DaLxGlService;
import com.ghxxzx.dagl.utils.WsFlag;
import com.ghxxzx.dagl.vo.ResultEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Api(value = "系统管理")
@RestController
@RequestMapping("/dagl/api")
public class DaLxGlController {
    Logger log = LoggerFactory.getLogger(getClass());
    @Autowired
    private DaLxGlService daLxGlService;
    @Autowired
    private WsFlag wsFlag;
    @ApiOperation(value = "新增档案类型",notes = "添加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dalxName",value = "档案类型名",required = true,paramType = "query",dataType = "dalxName"),
            @ApiImplicitParam(name = "parentId",value = "档案类型父Id",required = true,paramType = "query",dataType = "dalxName")
    })
    @RequestMapping(value = "addDalx")
    public ResultEntity addDalx(@RequestBody Dalx dalx){
        try {
            return daLxGlService.addDalx(dalx.getDalxName(),dalx.getParentId(),dalx.getCode());
        }  catch (JyException e) {
            log.error(e.getMess(), e);
            return ResultEntity.error(e.getMess());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResultEntity.error("未知异常");
        }
    }
    @ApiOperation(value = "查询档案类型节点",notes = "查询")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "dalxName",value = "档案类型名",required = true,paramType = "query",dataType = "dalxName"),
//            @ApiImplicitParam(name = "parentId",value = "档案类型父Id",required = true,paramType = "query",dataType = "dalxName")
//    })
    @RequestMapping(value = "queryDalx",method = RequestMethod.GET)
    public ResultEntity queryDalxNode(){
        try {
            return daLxGlService.queryDalxNode();
        } catch (JyException e) {
            log.error(e.getMess(), e);
            return ResultEntity.error(e.getMess());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResultEntity.error("未知异常");
        }
    }
    @ApiOperation(value = "查询档案类型节点",notes = "查询")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "dalxName",value = "档案类型名",required = true,paramType = "query",dataType = "dalxName"),
//            @ApiImplicitParam(name = "parentId",value = "档案类型父Id",required = true,paramType = "query",dataType = "dalxName")
//    })
    @ApiImplicitParam(name = "id",value = "档案类型Id",required = true,paramType = "query",dataType = "id")
    @RequestMapping(value = "deleteDalx")
    public ResultEntity deleteDalx(Integer id){
        try {
            return daLxGlService.deleteDalx(id);
        }  catch (JyException e) {
            log.error(e.getMess(), e);
            return ResultEntity.error(e.getMess());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResultEntity.error("未知异常");
        }
    }

    @ApiOperation(value = "查询所有档案类型节点",notes = "查询")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "dalxName",value = "档案类型名",required = true,paramType = "query",dataType = "dalxName"),
//            @ApiImplicitParam(name = "parentId",value = "档案类型父Id",required = true,paramType = "query",dataType = "dalxName")
//    })
    @RequestMapping(value = "queryAllLx",method = RequestMethod.GET)
    public ResultEntity queryAllLx(){
        try {
            return ResultEntity.ok(daLxGlService.findAll());
        }  catch (JyException e) {
            log.error(e.getMess(), e);
            return ResultEntity.error(e.getMess());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResultEntity.error("未知异常");
        }
    }
    @Autowired
    private DalxMapper dalxMapper;
    @RequestMapping(value = "queryCode")
    public ResultEntity queryCode(String code){
        try {
            QueryWrapper<Dalx> dalxQueryWrapper = new QueryWrapper<>();
            dalxQueryWrapper.eq("code",code);
            List<Dalx> list = dalxMapper.selectList(dalxQueryWrapper);
            if (list==null||list.size()==0){
                return ResultEntity.error("没有查到数据");
            }
            Dalx dalx = list.get(0);
            HashMap<Integer, Integer> hashMap = wsFlag.getDalxFlag();
            Integer lx = hashMap.get(dalx.getId());
            dalx.setSfws(lx);
            return ResultEntity.ok(dalx);
        } catch (JyException e) {
            log.error(e.getMess(), e);
            return ResultEntity.error(e.getMess());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResultEntity.error("未知异常");
        }
    }
    @RequestMapping(value = "queryKjWsFlag")
    public ResultEntity queryKjWsFlag(String dabh){
        try {
//            String code = wsFlag.getCode(dabh);
//            Integer byCode = dalxMapper.findByCode(code);
            Integer byCode = wsFlag.getTypeId(dabh);
            HashMap<Integer, Integer> hashMap = wsFlag.getDalxFlag();
            Integer lx = hashMap.get(byCode);
            return ResultEntity.ok(lx);
        } catch (JyException e) {
            log.error(e.getMess(), e);
            return ResultEntity.error(e.getMess());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResultEntity.error("未知异常");
        }
    }
}
